//
//  SASelectVipsVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/10.
//

#import "SASelectVipsVC.h"
#import "SACommonMemberSearchAPI.h"
#import "SASelectMerchantsCell.h"
#import "SAVipModel.h"

@interface SASelectVipsVC () <UITextFieldDelegate, HTTPAPIDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UITextField *searchTf;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SACommonMemberSearchAPI *commonMemberSearchAPI;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *tipView;
@property (nonatomic, strong) SAVipModel *vipModel;

@end

@implementation SASelectVipsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.commonMemberSearchAPI == api) {
        self.dataArray = [SAVipModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        [self.tableView reloadData];
        self.tipView.hidden = (self.dataArray.count > 0);
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    self.tipView.hidden = NO;
    [self.dataArray removeAllObjects];
    [self.tableView reloadData];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SASelectMerchantsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SASelectMerchantsCell" forIndexPath:indexPath];
    SAVipModel *model = self.dataArray[indexPath.row];
    if (model.mobile.length > 0) {
        cell.titleLab.text = [NSString stringWithFormat:@"%@(%@)",model.memberName,model.mobile];
    }else {
        cell.titleLab.text = model.memberName;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SAVipModel *model = self.dataArray[indexPath.row];
    !self.selectBlock ?: self.selectBlock(model);
    [self.navigationController popViewControllerAnimated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(0.01);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return SafeFooterHeight;
}

#pragma mark - UITextFieldDelegate
- (void)textFieldEditChanged:(UITextField*)textField {
    self.commonMemberSearchAPI.memberName = textField.text;
    [self.commonMemberSearchAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBI:@"" AndTitle:@""];
    [self setBgImage:nil];
    
    [self addRightViews:@[self.topView]];
    [self.topView addSubview:self.searchTf];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.tipView];
    
    self.commonMemberSearchAPI.memberName = @"";
    [self.commonMemberSearchAPI sendRequestToServerWithActivityViewAndMask:GET];
}

#pragma mark - lazy
-(UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:SARectMake(63, 0, 312, 44)];
    }
    return _topView;
}

-(UITextField *)searchTf {
    if (!_searchTf) {
        _searchTf = [[UITextField alloc] initWithFrame:SARectMake(0, 7, 249, 30)];
        _searchTf.placeholder = @"输入会员名称/手机号";
        _searchTf.text = @"";
        _searchTf.backgroundColor = [UIColor whiteColor];
        _searchTf.layer.cornerRadius = kScaleNum(5);
        _searchTf.layer.masksToBounds = YES;
        _searchTf.delegate = self;
        _searchTf.font = kFont(14);
        _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
        UIView *leftView = [[UIView alloc] initWithFrame:SARectMake(0, 0, 26, 30)];
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:SARectMake(6, 7, 14, 16)];
        imageV.image = [UIImage imageNamed:@"shop_search"];
        [leftView addSubview:imageV];
        _searchTf.leftView = leftView;
        _searchTf.leftViewMode = UITextFieldViewModeAlways;
        [_searchTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTf;
}

-(SACommonMemberSearchAPI *)commonMemberSearchAPI {
    if (!_commonMemberSearchAPI) {
        _commonMemberSearchAPI = [SACommonMemberSearchAPI apiWithDelegate:self];
    }
    return _commonMemberSearchAPI;
}


-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight)) style:UITableViewStylePlain];
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SASelectMerchantsCell class] forCellReuseIdentifier:@"SASelectMerchantsCell"];
    }
    return _tableView;
}

-(NSMutableArray *)dataArray
{
    if(_dataArray == nil)
    {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UIView *)tipView {
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight))];
        _tipView.backgroundColor = [UIColor whiteColor];
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Shop_search2"]];
        icon.frame = SARectMake(124, 147 - StatusHeight, 138, 83);
        [_tipView addSubview:icon];
        
        UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(0, 240 - StatusHeight, kBaseScreenWidth, 30)];
        titleLab.text = @"暂无相关信息";
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = kGrayColor;
        [_tipView addSubview:titleLab];
    }
    return _tipView;
}

@end
